'use strict';

const uuid = require("uuid")

const db = uniCloud.database()
exports.main = async (event, context) => {
	let {accountNumber, password, role} = event;
	
	try{
		// 检查用户名是否已存在
		const user = await db.collection('users').where({
			accountNumber
		}).get()
		
		if (user.data.length > 0) {
			return {
			  code: 400,
			  message: '该用户已存在'
			}
		}
		
		
		await db.collection("users").add({
			accountNumber,
			password,
			uuid: uuid(),
			role,
			studyNewTimes: 0,
			reviewTimes: 0,
		});
		
		return {
			code: 200,
			message: '注册成功'
		}
	} catch(e) {
		return e
	}
	


};
